package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouTargetMarkData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouTargetMarkDataDaoImpl extends TempDataObjectDao<CrossXingYouTargetMarkData> {

    public long getMaxId(int activityId, int groupId) {
        String sql = "select max(id) as maxId from t_c_xing_you_team_mark_data where activityId=? and groupId = ?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                long data = rs.getLong("maxId");
                if(data == 0){
                    return 1;
                }
                return data + 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return 1;
    }

    public static String getCrossXingYouTargetMarkDataSql = "select * from `t_c_xing_you_team_mark_data` where `activityId`=? and groupId = ?";
    public Map<Integer, List<CrossXingYouTargetMarkData>> getCrossXingYouTargetMarkData(int activityId, int groupId, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<CrossXingYouTargetMarkData>> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouTargetMarkDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouTargetMarkData data = parse(rs);
                if(data.getGetReward() == 1){
                    continue;
                }
                if(!resultMap.containsKey(data.getTeamId())){
                    resultMap.put(data.getTeamId(), new ArrayList<>());
                }
                List<CrossXingYouTargetMarkData> list = resultMap.get(data.getTeamId());
//                if(list.size() >= limit){
//                    continue;
//                }
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public static String updateCrossXingYouTargetMarkDataSql = "update `t_c_xing_you_team_mark_data` set teamId=?,userId=?,pos=?,targetUserId=?,maxTargetHp=?,targetHp=?,tzList=?,logTime=?,expireTime=?,getReward=? where activityId=? and groupId = ? and id=?";
    @Override
    protected boolean updateData(CrossXingYouTargetMarkData crossXingYouTargetMarkData) {
        crossXingYouTargetMarkData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getMaxTargetHp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getTargetHp()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(crossXingYouTargetMarkData.getTzList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getLogTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getExpireTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getGetReward()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getId()));
        if (getDb().executeNoneQuery(updateCrossXingYouTargetMarkDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXingYouTargetMarkDataSql = "insert into `t_c_xing_you_team_mark_data`(activityId,groupId,id,teamId,userId,pos,targetUserId,maxTargetHp,targetHp,tzList,logTime,expireTime,getReward) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouTargetMarkData crossXingYouTargetMarkData) {
        crossXingYouTargetMarkData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getPos()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getMaxTargetHp()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getTargetHp()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(crossXingYouTargetMarkData.getTzList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getLogTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouTargetMarkData.getExpireTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouTargetMarkData.getGetReward()));
        if (getDb().executeNoneQuery(insertCrossXingYouTargetMarkDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouTargetMarkData parseData(ResultSet rs) throws SQLException {
        CrossXingYouTargetMarkData crossXingYouTargetMarkData = new CrossXingYouTargetMarkData();
        crossXingYouTargetMarkData.setActivityId(rs.getInt("activityId"));
        crossXingYouTargetMarkData.setId(rs.getLong("id"));
        crossXingYouTargetMarkData.setTeamId(rs.getInt("teamId"));
        crossXingYouTargetMarkData.setUserId(rs.getLong("userId"));
        crossXingYouTargetMarkData.setPos(rs.getInt("pos"));
        crossXingYouTargetMarkData.setTargetUserId(rs.getLong("targetUserId"));
        crossXingYouTargetMarkData.setMaxTargetHp(rs.getLong("maxTargetHp"));
        crossXingYouTargetMarkData.setTargetHp(rs.getLong("targetHp"));
        crossXingYouTargetMarkData.setTzList(StringUtils.stringToLongList(rs.getString("tzList"), ","));
        crossXingYouTargetMarkData.setLogTime(rs.getLong("logTime"));
        crossXingYouTargetMarkData.setExpireTime(rs.getLong("expireTime"));
        crossXingYouTargetMarkData.setGetReward(rs.getInt("getReward"));
        crossXingYouTargetMarkData.setGroupId(rs.getInt("groupId"));

        return crossXingYouTargetMarkData;
    }
}
